﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Mobile_H.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Cart_Tag
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <style type="text/css">
        input.ui-input-text
        {
            height: 25px;
            background-color: #FFF;
            border: 1px solid #bbbbbb;
            -webkit-box-shadow: 0 1px 3px rgba(0,0,0,0.3) inset;
            -moz-box-shadow: 0 1px 3px rgba(0,0,0,0.3) inset;
            box-shadow: 0 1px 3px rgba(0,0,0,0.3) inset;
            width: 95%;
        }
        
        tr
        {
            height: 35px;
            text-align: right;
            background-color: #fff;
        }
        tr.dis
        {
            background-color: #FFFFC2;
        }
    </style>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="FeaturedContent" runat="server">
    <div style="background-color: #8094AE; height: 50px; margin-top: 0px;">
        <h2 style="color: White; vertical-align: middle;">
            &nbsp;COUNT</h2>
    </div>
    <div id="page1">
        <table style="width: 100%; font-size: medium; color: #004080; padding: 5px; font-weight: bold;">
            <tr class="dis">
                <td style="width: 30%; text-align: right;">
                    DATE : &nbsp;
                </td>
                <td style="text-align: left;">
                    &nbsp;&nbsp;
                    <%= WinNemoLib.DateUtil.ToDisplayDate(DateTime.Now) %>
                </td>
            </tr>
            <tr class="dis">
                <td>
                    EMP : &nbsp;
                </td>
                <td style="text-align: left;">
                    &nbsp;&nbsp;
                    <%= User.Identity.Name.ToString() %>
                </td>
            </tr>
            <tr class="dis">
                <td>
                    Area : &nbsp;
                </td>
                <td style="text-align: left;">
                    &nbsp;&nbsp;
                    <%= ViewBag.area %>
                </td>
            </tr>
            <tr>
                <td>
                    Cart : &nbsp;
                </td>
                <td style="text-align: left;">
                    <input type="text" id="txtcart" class="ui-input-text" />
                </td>
            </tr>
            <tr>
                <td>
                    Tag : &nbsp;
                </td>
                <td style="text-align: left;">
                    <input type="password" id="txttag" class="ui-input-text" />
                </td>
            </tr>
        </table>
        <div style="float: left">
            <input type="button" id="back" value="Back" style="width: 40%" /></div>
        <div style="float: right; width: 45%">
            <input type="button" id="blank" value="&nbsp;Blank Cart&nbsp;" style="width: 100%" /></div>
    </div>
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="ScriptsSection" runat="server">
    <script type="text/jscript">

        $(function () {
            $("#txtcart").autoNumeric({ vMin: '0', vMax: '999', aPad: false });
            setTimeout(function () { $("#txtcart").focus(); }, 1000);
            $("#txttag").keypress(function (evt) {
                evt = (evt) ? evt : (window.event) ? event : null;
                if (evt) {
                    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
                    if (charCode == 13) {
                        Add_Data();
                    }
                }
            });

            $("#txttag").keydown(function (evt) {
                if (evt.keyCode == 13) {
                    Add_Data();
                }
            });
            function Comp_cart() {
                var cartno = $("#txtcart").val();
                $.post('<%= Url.Action("GetCartno") %>' + '?tag=' + encodeURIComponent($("#txttag").val())
            , function (data) {
                if (cartno != data) {
                    alert("This tag doesn't match cart no. "+cartno);
                    return false;
                }
                return true;
            });
            }

            function Add_Data() {
                if ($("#txtcart").val() == "") {

                    setTimeout(function () { $("#txtcart").focus(); }, 1000);
                    alert("Input Data");
                    return;
                }
                if ($("#txttag").val() == "") {
                    alert("Input Data");
                    setTimeout(function () { $("#txttag").focus(); }, 1000);
                    return;
                }
                $.post('<%= Url.Action("CartInDB_Count") %>' + '?cartno=' + $("#txtcart").val() + '&notuse=true', null
                    , function (data) {
                        if (data.success == "2") {
                            alert(data.message);
                            window.location = '<%: Url.Action("Exit_data") %>' + '?cart=' + $("#txtcart").val() + '&area=' + '<%= ViewBag.area %>' + '&tag=' + encodeURIComponent($("#txttag").val());
                        }
                        if (data.success == "0") {
                            alert(data.message);
                            $("#txtcart").val("").focus();
                        }
                        if (data.success == "1") {
                            $.post('<%= Url.Action("GetTag") %>' + '?tag=' + encodeURIComponent($("#txttag").val()), function (data) {
                                if (data != "") {
                                    alert(data);
                                    $("#txttag").val("");
                                    return;
                                }
                                else {
                                    Comp_cart();
                                    window.location = '<%= Url.Action("Tag_Cart") %>' + '?tag=' + encodeURIComponent($("#txttag").val()) + "&area=" + '<%= ViewBag.area %>' + '&cartno=' + $("#txtcart").val();
                                }
                            }); //post Check Tag
                        }
                    }); //post Check Cart             
                }
                $("#back").click(function () {
                    window.location = '<%= Url.Action("Count") %>';
                });

                $("#blank").click(function () {
                    if ($("#txtcart").val() == "") {
                        setTimeout(function () { $("#txtcart").focus(); }, 1000);
                        alert("Input Data");
                        return;
                    }
                    if (!confirm("Confirm Blank Cart?")) {
                        return false;
                    }
                    Save_blank();
                });
                function Save_blank() {

                    $.post('<%= Url.Action("CartInDB_Count") %>' + '?cartno=' + $("#txtcart").val() + '&notuse=true', null
                    , function (data) {
                        if (data.success == "2") {
                            alert(data.message);
                            window.location = '<%: Url.Action("Exit_data") %>' + '?cart=' + $("#txtcart").val() + '&area=' + '<%= ViewBag.area %>' + '&tag=' + encodeURIComponent($("#txttag").val());
                        }
                        if (data.success == "0") {
                            alert(data.message);
                            $("#txtcart").val("").focus();
                        }
                        if (data.success == "1") {
                            var dataForSubmit = new Array();
                            dataForSubmit.push({
                                _area: '<%= ViewBag.area %>'
                            });
                            var getUsersUrl = '<%= Url.Action("Blank_Cart") %>';
                            callScriptMethod(getUsersUrl, { theData: dataForSubmit, cartno: $("#txtcart").val() });
                        }
                    });

                }

                function callScriptMethod(url, jsonObject, action, callback, async) {

                    callback = callback || function () { };
                    async = (async == null || async);

                    $.ajax({
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        url: url,
                        data: JSON.stringify(jsonObject),
                        dataType: "json",
                        async: async,
                        success: function (data) {

                            if (data.success != "1") {
                                alert(data.message);
                            } else {

                                alert("Save Complete");
                                window.location = '<%= Url.Action("Cart_Tag") %>' + '?area=' + '<%= ViewBag.area %>'
                            }
                        },
                        error: function (xhr, status, error) {
                            var err = xhr.responseText;
                            alert(err.Message);
                        }
                    });
                }
        });

    </script>
</asp:Content>
